package com.oglvip.business.web.controller;

import com.oglvip.business.consts.common.ResponseCode;
import com.oglvip.business.dto.GoodsFormDto;
import com.oglvip.business.dto.PageBean;
import com.oglvip.business.dto.ResponseData;
import com.oglvip.business.remote.service.api.GoodsService;
import com.oglvip.business.remote.service.entity.Goods;
import com.oglvip.business.remote.service.entity.PageData;
import com.oglvip.business.remote.service.entity.StoreAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther hanchao
 * @date 2017/1/8 20:25
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("/categoryList")
    public ResponseData categoryList(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        return goodsService.categoryList(account.getStoreId());
    }

    /**
     * 分页获取商品列表
     * @param pageBean
     * @param goodsName
     * @param categoryId
     * @param status
     * @return
     */
    @RequestMapping("/pageList")
    public ResponseData pageList(PageBean pageBean,String goodsName,Integer categoryId,Integer status){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        ResponseData<PageData<Goods>> resp = goodsService.list(account.getTicket(),account.getStoreId(),goodsName,categoryId,status,pageBean.getFirstResult(),pageBean.getOnePageSize());
        PageData<Goods> pageData = resp.get();
        ResponseData result = new ResponseData();
        result.setResponseCode(ResponseCode.SUCCESS);
        pageBean.setTotalResults(pageData.getSize());
        pageBean.setData(pageData.getList());
        result.setData(pageBean);
        return result;
    }

    @RequestMapping("/add")
    public ResponseData addGoods(GoodsFormDto goods){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        goods.setTicket(account.getTicket());
        return goodsService.addGoods(goods);
    }

    @RequestMapping("/set")
    public ResponseData setGoods(GoodsFormDto goods){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        goods.setTicket(account.getTicket());
        return goodsService.setGoods(goods);
    }

    @RequestMapping("/udpateStatus")
    public ResponseData updateStatus(String goodsIds,Integer status){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StoreAccount account = (StoreAccount) authentication.getPrincipal();
        return goodsService.updateStatus(account.getTicket(), goodsIds,status);
    }

    @RequestMapping("/get")
    public ResponseData getGoods(Integer goodsId){
        return goodsService.getGoods(goodsId);
    }
}
